/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.NodeNameDisplayEditPart;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagram.activity.figure.PinStringFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Controller of an pin name
 *
 * @author Gilles Cannenterre
 * @since 28 mars 07
 */
public class PinNameDisplayEditPart extends NodeNameDisplayEditPart {


	/**
	 * Create a FeatureList EditPart accepting any kind from drag and drop.
	 * 
	 * @param modelManager 
	 * @param graphnode 
	 */

	public PinNameDisplayEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);

	}

	/**
	 * Create a FeatureList EditPart accepting specified type from drag and drop.
	 * Create a ListEditPart for a List accepting only specified element.
	 * 
	 * @param element 
	 * @param modelManager 
	 * @param elementType 	 * @param graphnode 
	 */
	public PinNameDisplayEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Element element,ModelManager modelManager) {
		super(graphnode,element,modelManager);

	}

	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getFigure().getPreferredSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);
		PinStringFigure fig = (PinStringFigure)getFigure();
		
		if (getUmlElement() instanceof NamedElement) {
			fig.setText(((NamedElement)getUmlElement()).getName());
		}

		GraphNode parentGN = (GraphNode)getGraphNode().getSemanticParent();
		if (parentGN != null){			
			// Check if presenation of parent is a shape...
			boolean isParentShapeFigure = false;
			if (parentGN.getSemanticModel() != null) {
				isParentShapeFigure = parentGN.getSemanticModel().getPresentation().equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION);
			}
			
			if (!isParentShapeFigure) {
				fig.refreshColors(parentGN);
				fig.setVisible(true);	
			} else {
				fig.setVisible(false);
			}					
		}
		((GraphicalEditPart) getParent()).setLayoutConstraint(this,fig,r);
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure() {
		return new PinStringFigure(((NamedElement)getUmlElement()).getName());
	}
}